<?php
class Admin_controller_goods extends Core_controller_abstract
{
	public function index()
	{
		$goods = new Admin_model_goods();
		$goodsList = $goods->getGoodsForCateID(0);
		$this->view->assign('goodsList', $goodsList);
		$this->view->assign('title', '商品列表');
	}
	
	public function add()
	{
		$cate = new Admin_model_category();
		$cates = $cate->getAll();
		
		$this->view->assign('cates', $cates);
		$this->view->assign('title', '添加商品');
	}
	
	public function addSave()
	{
		if($this->requstMethod == 'POST')
		{
			$goods = new Admin_model_goods();
			
			$upload = new Core_utils_upload();
			$upload->upload(DOC_ROOT.'/UploadFiles/');
			
			if($upload->getErrorMsg() != '')
			{
				exit($upload->getErrorMsg());
			}
			
			if($goods->add($_POST))
			{
				$this->_redirect('admin.php?m=goods');
			}
		}
		else
		{
			$this->_redirect('admin.php?m=goods&a=add');
		}
	}
	
	public function modify()
	{
		$goods = new Admin_model_goods();
		
		$goodsID = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$goodsInfo = array();
		if($goodsID > 0)
		{
			$goodsInfo = $goods->getGoods($goodsID);
		}
		if(empty($goodsInfo))
		{
			$this->_redirect('admin.php?m=goods');
		}
		
		$cate = new Admin_model_category();
		$cates = $cate->getAll();
		
		$this->view->assign('cates', $cates);
		$this->view->assign('goodsInfo', $goodsInfo);
		$this->view->assign('title', '编辑商品');
	}
}